package com.zds.hj.poseidon.biz.dispatcher.credit;

import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.boot.dispatch.dto.BaseResultModel;
import com.zds.boot.dispatch.dto.ModuleBaseInfo;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;

import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.poseidon.facade.dto.hpp.credit.enums.OrderStateEnum;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.zly.common.enums.HjResultCodeEnum;
import com.zds.zly.common.utils.EmptyChecker;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@DispatcherHandler(value = "CreditEntConstraintHandler", desc = "授信企业约束模块",applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class CreditEntConstraintHandler extends CreditCheckAbsHandler{


    @Resource
    private CreditApplyService creditApplyService;

    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {
        log.info("授信企业约束模块-------------------开始执行");
        //调用第三方接口 看是否该企业存在在途信息 是否有未结清的数据 有直接抛出异常
        List<CreditInfoEntityDto> credits = creditApplyService.findCreditByCreditNo(dbData);
        if (EmptyChecker.isEmpty(credits)){
            throw new BusinessException("未查到相关授信信息", HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
        }
        //过滤出集合中的订单状态即 在授信中 或者 待还款
        List<CreditInfoEntityDto> collects = credits.stream().filter(i -> i.getOrderStateEnum() == OrderStateEnum.PROGRESS_CRAFT || i.getOrderStateEnum() == OrderStateEnum.WAIT_REPAY_MONEY).collect(Collectors.toList());
        for(CreditInfoEntityDto credit : collects){
            if (credit.getOrderStateEnum()==OrderStateEnum.PROGRESS_CRAFT){
                throw new BusinessException("授信审批中,还需等待",HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
            }
            if (credit.getOrderStateEnum()==OrderStateEnum.WAIT_REPAY_MONEY){
                throw new BusinessException("请先还清贷款,在进行其他相关的操作",HjResultCodeEnum.PARAMS_VALIDATE_FAIL.code());
            }
        }

    }

    /**
     * 将其变成自动模块
     * @return
     */
    @Override
    public ModuleBaseInfo moduleBaseInfo(){
        ModuleBaseInfo moduleBaseInfo = super.moduleBaseInfo();
        moduleBaseInfo.setAuto(true);
        return moduleBaseInfo;
    }

}
